package com.sanyou.controller.api;

import com.sanyou.pojo.CopperParticleCheckRecord;
import com.sanyou.pojo.dto.req.VerticalityCheckRecordUploadReq;
import com.sanyou.service.CacheService;
import com.sanyou.service.CopperParticleService;
import com.sanyou.service.VerticalityCheckRecordService;
import com.sanyou.utils.JSONResult;
import com.sanyou.utils.SnowFlake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author xhy
 * @Description 阴极板相关的对外接口
 */
@Api(value = "阴极板相关的对外接口",tags = {"阴极板相关的Controller"})
@CrossOrigin
@RestController
@RequestMapping("/api/product")
public class ApiProductController {
    @Autowired
    private CacheService cacheService;

    @Autowired
    private CopperParticleService copperParticleService;

    @Autowired
    private VerticalityCheckRecordService verticalityCheckRecordService;

    @ApiOperation(value = "铜粒子检测记录上传", notes = "铜粒子检测记录上传")
    @PostMapping("/copperParticleUpload")
    public JSONResult copperParticleUpload(@RequestBody CopperParticleCheckRecord record){
        //数据合法性判断
        if(record==null){
            return JSONResult.errorMsg("上传记失败：数据不能为空");
        }
        if(StringUtils.isEmpty(record.getDeviceNo())){
            return JSONResult.errorMsg("上传记失败：设备编号不能为空");
        }

        if(StringUtils.isEmpty(record.getDatetime())){
            record.setDatetime(new Date());
        }

        try {
                //将记录存入数据库
                copperParticleService.insertRecord(record);
            return JSONResult.ok("上传记录成功");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.errorMsg("上传失败:"+e.getMessage());
        }
    }

    @ApiOperation(value = "垂直度检测记录上传", notes = "垂直度检测记录上传")
    @PostMapping("/verticaltiyUpload")
    public JSONResult verticaltiyUpload(@RequestBody VerticalityCheckRecordUploadReq req){
        //数据合法性判断
        if(req==null){
            return JSONResult.errorMsg("上传记失败：数据不能为空");
        }
        if(StringUtils.isEmpty(req.getProductCode())){
            return JSONResult.errorMsg("上传记失败：阴极板编号不能为空");
        }
        if(req.getMaxBias()==null){
            return JSONResult.errorMsg("上传记失败：最大偏差不能为空");
        }

        try {
            //将记录存入数据库
            verticalityCheckRecordService.uploadRecord(req);
            return JSONResult.ok("上传记录成功");
        }catch (Exception e){
            return JSONResult.errorMsg("上传失败:"+e.getMessage());
        }
    }


    @ApiOperation(value = "ping", notes = "连通性测试")
    @GetMapping("/ping")
    public JSONResult ping(){
        return JSONResult.ok("ok");
    }
}
